package com.ruoyi.pingjiao.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.pingjiao.domain.EvaluationPaper;
import com.ruoyi.pingjiao.service.IEvaluationService;
import com.ruoyi.pingjiao.service.IStudentService;
import com.ruoyi.system.service.ISysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.pingjiao.domain.EvalutionActivity;
import com.ruoyi.pingjiao.service.IEvalutionActivityService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 评教活动管理Controller
 *
 * @author 胡浩鹏
 * @date 2022-10-22
 */
@Api("评教活动管理")
@RestController
@RequestMapping("/pingjiao/activity")
public class EvalutionActivityController extends BaseController
{
    @Autowired
    private IEvalutionActivityService evalutionActivityService;
    @Autowired
    private IEvaluationService evaluationService;
    @Autowired
    private ISysConfigService sysConfigService;
    @Autowired
    private IStudentService studentService;
    /**
     * 查询评教活动管理列表
     */
    @PreAuthorize("@ss.hasPermi('pingjiao:activity:list')")
    @GetMapping("/list")
    public TableDataInfo list(EvalutionActivity evalutionActivity)
    {
        startPage();
        List<EvalutionActivity> list = evalutionActivityService.selectEvalutionActivityList(evalutionActivity);
        return getDataTable(list);
    }

    /**
     * 导出评教活动管理列表
     */
    @PreAuthorize("@ss.hasPermi('pingjiao:activity:export')")
    @Log(title = "评教活动管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, EvalutionActivity evalutionActivity)
    {
        List<EvalutionActivity> list = evalutionActivityService.selectEvalutionActivityList(evalutionActivity);
        ExcelUtil<EvalutionActivity> util = new ExcelUtil<EvalutionActivity>(EvalutionActivity.class);
        util.exportExcel(response, list, "评教活动管理数据");
    }

    /**
     * 获取评教活动管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('pingjiao:activity:query')")
    @GetMapping(value = "/{evalutionActivityId}")
    public AjaxResult getInfo(@PathVariable("evalutionActivityId") Long evalutionActivityId)
    {
        return AjaxResult.success(evalutionActivityService.selectEvalutionActivityByEvalutionActivityId(evalutionActivityId));
    }

    /**
     * 新增评教活动管理
     */
    @PreAuthorize("@ss.hasPermi('pingjiao:activity:add')")
    @Log(title = "评教活动管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody EvalutionActivity evalutionActivity)
    {
        return toAjax(evalutionActivityService.insertEvalutionActivity(evalutionActivity));
    }

    /**
     * 修改评教活动管理
     */
    @PreAuthorize("@ss.hasPermi('pingjiao:activity:edit')")
    @Log(title = "评教活动管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody EvalutionActivity evalutionActivity)
    {
        return toAjax(evalutionActivityService.updateEvalutionActivity(evalutionActivity));
    }

    /**
     * 删除评教活动管理
     */
    @PreAuthorize("@ss.hasPermi('pingjiao:activity:remove')")
    @Log(title = "评教活动管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{evalutionActivityIds}")
    public AjaxResult remove(@PathVariable Long[] evalutionActivityIds)
    {
        return toAjax(evalutionActivityService.deleteEvalutionActivityByEvalutionActivityIds(evalutionActivityIds));
    }
    /**
     * 开启某个评价活动
     * @author  hpf
     */
    @PreAuthorize("@ss.hasPermi('pingjiao:activity:start')")
    @Log(title = "开启评教活动", businessType = BusinessType.UPDATE)
    @GetMapping("/start/{evalutionActivityId}")
    public AjaxResult start(@PathVariable Long evalutionActivityId)
    {
        int result  =evalutionActivityService.startEvaluationActivity(evalutionActivityId);
        if(result==1){
            return  AjaxResult.success("活动开启成功");
        }else if(result==0){
            return AjaxResult.error("活动已经是开启状态");
        }else if(result==-1) {
            return AjaxResult.error("有同类型的活动还在进行中...");
        }else{
            return AjaxResult.success("活动开启失败");
        }
    }
    /**
     * 统计并保存某个评价活动的所有教师的成绩
     * @author  hpf
     */
    @ApiOperation("手动统计学生评教分数")
    @PreAuthorize("@ss.hasPermi('pingjiao:activity:statistics')")
    @Log(title = "统计评教活动", businessType = BusinessType.UPDATE)
    @GetMapping("/statistics/{evalutionActivityId}")
    public AjaxResult statistics(@PathVariable Long evalutionActivityId){
        evaluationService.saveEvalScoreOfTeacher(evalutionActivityId);
        return AjaxResult.success("统计成功");
    }
    /**
     * 获取当前学期统计完成的活动列表，（按课程类型分类;1:普通教师；2：辅导员）
     * @author  hpf
     */
    @ApiOperation("当前学期统计完成的活动列表")
    @PreAuthorize("@ss.hasPermi('pingjiao:activity:list')")
    @GetMapping("/getList/{courseType}")
    public AjaxResult getListByCourseType(@PathVariable String courseType){
        String term=sysConfigService.selectConfigByKey("currentTerm");//当前学期，从系统中自动获取
        EvalutionActivity evalutionActivity=new EvalutionActivity();
        evalutionActivity.setCourseType(courseType);
        evalutionActivity.setTerm(term);
        evalutionActivity.setStatus("4");
        List<EvalutionActivity> list = evalutionActivityService.selectEvalutionActivityList2(evalutionActivity);
        return AjaxResult.success(list);
    }

    /**
     * 获取评教全部学期
     * @param status
     * @return
     */
    @GetMapping("term/{status}")
    public AjaxResult getTerm(@PathVariable String status){
        LoginUser loginUser= SecurityUtils.getLoginUser();
        Long studentId = studentService.selectStudentIdByUserId(loginUser.getUserId());
        List<String>list= evalutionActivityService.selectTermByStudentIdAndStatus(studentId,status);
        return AjaxResult.success(list);
    }
    @GetMapping("getPaper/{term}")
    public AjaxResult getActivityPaper(@PathVariable String term){
        LoginUser loginUser= SecurityUtils.getLoginUser();
        Long studentId = studentService.selectStudentIdByUserId(loginUser.getUserId());
        List<EvaluationPaper>list= evalutionActivityService.selectActivityPaperByTerm(studentId,term);
        return AjaxResult.success(list);
    }
}
